package com.nuyoah.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.nuyoah.common.dto.LoginDto;
import com.nuyoah.common.dto.UserDto;
import com.nuyoah.entity.Teacher;
import com.nuyoah.entity.User;
import com.nuyoah.mapper.TeacherMapper;
import com.nuyoah.mapper.UserMapper;
import com.nuyoah.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 存储教师的特定信息 服务实现类
 * </p>
 *
 * @author xyx
 * @since 2024-06-03
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public Teacher getByUserId(Integer id) {
        return teacherMapper.getByUserId(id);
    }

    @Override
    public UserDto login(LoginDto loginDto) {
        //根据loginDto.getUsername()到user表中查询是否有该用户
        //如果有，则根据user.id到teacher表中查询该教师的详细信息
        //如果没有，则返回null
        User teacher = userMapper.getUserByUsername(loginDto.getUsername());
        if (teacher == null) {
            return null;
        }

        StpUtil.login(teacher.getId());

        Teacher t = teacherMapper.getByUserId(teacher.getId());
        UserDto userDto = new UserDto();
        userDto.setId(teacher.getId());
        userDto.setUsername(teacher.getUsername());
        userDto.setAge(teacher.getAge());
        userDto.setGender(teacher.getGender());
        userDto.setPhone(teacher.getPhone());
        userDto.setEmail(teacher.getEmail());
        userDto.setTeachingExperience(t.getTeachingExperience());

        userDto.setToken(StpUtil.getTokenValue());
        return userDto;
    }
}
